{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Endianess Explained\n",
    "\n",
    "We need to be careful when parsing binary data into numbers.  When bytes are being interpreted then we must pay attention to whether the convention is BigEndian or LittleEndian.\n",
    "\n",
    "BigEndian has the most significant byte on the left.  That's the normal convention as seen by a normal person.  For example, 0x0001 is being represented in memory with 2 bytes 0x00 and 0x01 consecutively.\n",
    "\n",
    "LittleEndian has the most significant byte on the right.  In the above example, the number would be stored as 0x01 and 0x00 respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reinterpret(Int16, 0x0001)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the above example, we have to realize that `0x0001` is represented in the normal convention for human consumption but on a LittleEndian system it is actually stored as [0x01, 0x00].  Hence the number being represented is still 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1-element Array{Int16,1}:\n",
       " 256"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reinterpret(Int16, [0x00, 0x01])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, the array already represents memory locations.  On a LittleEndian system, it it would represent the number 0x0100 which is 256."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.1",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
